extends Panel

@export var grid_container : GridContainer
@export var slot_scene : PackedScene
@export var max_slots := 20

func _ready():
	Inventory.inventory_updated.connect(_update_ui)
	_create_slots()
	_update_ui()

func _create_slots():
	for i in range(max_slots):
		var slot = slot_scene.instantiate()
		slot.slot_index = i
		grid_container.add_child(slot)

func _update_ui():
	for i in range(max_slots):
		var slot = grid_container.get_child(i)
		if i < Inventory.items.size():
			slot.update_slot(Inventory.items[i])
			slot.disabled = false
		else:
			slot.update_slot(null)
			slot.disabled = true
		
		# 更新选中状态
		if i == Inventory.selected_slot_index:
			slot.add_theme_color_override("font_pressed_color", Color.RED)
		else:
			slot.add_theme_color_override("font_pressed_color", Color.WHITE)

func _on_add_button_pressed() -> void:
	Inventory.add_item("item1", 1)


func _on_remove_button_pressed() -> void:
	if Inventory.selected_slot_index != -1:
		Inventory.remove_item(Inventory.selected_slot_index)
